# coding=utf-8
"""
    @project: 15python_spider
    @Author：frank
    @file： 03_lianjia_xpath.py
    @date：2024/1/13 20:49
"""
import io
import random
import sys
import time
from lxml import etree

import requests

from day02.utils.UserAgent import user_agent_list


class LianjiaSpider:

    def __init__(self):
        self.url = 'https://quanzhou.lianjia.com/ershoufang/pg{}/'
        self.ua_list = user_agent_list

    def get_page(self, url):
        headers = {
            'User-Agent': random.choice(self.ua_list)
        }
        res = requests.get(url, headers=headers)
        res.encoding = 'utf-8'
        html = res.text
        self.parse_page(html)

    def parse_page(self, html):
        parse_html = etree.HTML(html)
        house_node_list = parse_html.xpath(
            '//ul[@class="sellListContent"]/li[@class="clear LOGCLICKDATA"] | //ul[@class="sellListContent"]/li[@class="clear LOGVIEWDATA LOGCLICKDATA"]')
        for house_node in house_node_list:
            title = house_node.xpath(".//div[@class='info clear']/div[@class='title']/a/text()")
            price = house_node.xpath(
                ".//div[@class='info clear']/div[@class='priceInfo']//div[@class='totalPrice totalPrice2']/span/text()")
            print(title, price)

    def main(self):
        for i in range(1, 10):
            self.get_page(self.url.format(i))
            time.sleep(random.randint(1, 5))


if __name__ == "__main__":
    start = time.time()
    spider = LianjiaSpider()
    spider.main()
    end = time.time()
    print('执行时间:%.2f' % (end - start))
